<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>统计数据</title>
    <link rel="stylesheet" href="/static/layui/css/layui.css">
    <style>
        .container {
            width: 96%;
            margin: 0 auto;
        }
        .chart-container {
            display: flex;
            justify-content: space-around;
            margin-top: 20px;
        }
        .chart {
            width: 45%;
            height: 400px;
        }
        .update-btn {
            margin-top: 20px;
            text-align: center;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="layui-row">
            <div class="layui-col-md12">
                <h2 class="layui-text">数据汇总</h2>
            </div>
        </div>
        <div class="layui-row chart-container">
            <div class="layui-col-md6 chart" id="attackLevelChart"></div>
            <div class="layui-col-md6 chart" id="riskLevelChart"></div>
        </div>
        <div class="layui-row update-btn">
            <button class="layui-btn layui-btn-normal" id="updateBtn">更新数据</button>
        </div>
    </div>

    <script src="/static/layui/layui.js" type="text/javascript" charset="utf-8"></script>
    <script src="/static/echars/echarts.min.js"></script>
    <script>
        layui.use(['element'], function(){
            var element = layui.element;

            // 初始化饼图
            var attackLevelChart = echarts.init(document.getElementById('attackLevelChart'));
            var riskLevelChart = echarts.init(document.getElementById('riskLevelChart'));

            // 加载初始数据
            updateData();
 // 绑定按钮点击事件
            document.getElementById('updateBtn').addEventListener('click', updateData);
            function updateData() {
                fetch('/getSumData')
                    .then(response => response.json())
                    .then(data => {
                        console.log('update');
                        renderCharts(data);
                    });
            }

            function renderCharts(data) {
                var attackLevelOption = {
                    title: {
                        text: '攻击等级分布',
                        left: 'center'
                    },
                    tooltip: {
                        trigger: 'item'
                    },
                    series: [{
                        name: '攻击等级',
                        type: 'pie',
                        radius: '50%',
                        data: data.attack_level_data.labels.map((label, index) => ({
                            value: data.attack_level_data.data[index],
                            name: label
                        })),
                        label: {
                            show: true,
                            formatter: '{b}: {c}'
                        },
                        labelLine: {
                            show: true
                        },
                        emphasis: {
                            itemStyle: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }]
                };

                var riskLevelOption = {
                    title: {
                        text: '风险等级分布',
                        left: 'center'
                    },
                    tooltip: {
                        trigger: 'item'
                    },
                    series: [{
                        name: '风险等级',
                        type: 'pie',
                        radius: '50%',
                        data: data.risk_level_data.labels.map((label, index) => ({
                            value: data.risk_level_data.data[index],
                            name: label
                        })),
                        label: {
                            show: true,
                            formatter: '{b}: {c}'
                        },
                        labelLine: {
                            show: true
                        },
                        emphasis: {
                            itemStyle: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }]
                };

                attackLevelChart.setOption(attackLevelOption);
                riskLevelChart.setOption(riskLevelOption);
            }
        });
    </script>
</body>
</html>